package com.zsix.system.strategy.userByTaskRule;

import com.alibaba.fastjson.JSONObject;
import com.zsix.system.mapper.SqlMapper;
import com.zsix.system.mapper.taskAssigneeMapper.GenerWorkMapper;
import com.zsix.system.service.IAssigneeUserService;
import com.zsix.common.entity.taskAssignee.AssigneeUserEntity;
import com.zsix.common.entity.taskAssignee.GenerWork;
import com.zsix.common.util.RedisUtils;
import com.zsix.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 根据sql语句查找节点处理人
 */
@Component
public class BySql implements UserByTaskRuleStrategy {
    @Autowired
    private IAssigneeUserService assigneeUserService;
    @Autowired
    private GenerWorkMapper generWorkMapper;
    @Autowired
    private SqlMapper sqlMapper;
    @Autowired
    private RedisUtils redisUtils;
    public HashMap<String,Object> getUserByTaskRule(String taskid, String ruleName
            , HashMap<String,Object> variables, String procInstId) {
        //获取设置的接收人数据
        HashMap<String, Object> hashMap = assigneeUserService
                .getAssigneeUserWithTaskKey(taskid, ruleName);
        List<AssigneeUserEntity> assigneeUserEntityList = (List<AssigneeUserEntity>)hashMap.get("assigneeUserEntityList");
        //获取当前登陆人信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        String nextWorkEmp = "";
        for (AssigneeUserEntity ae : assigneeUserEntityList) {
            String sql=ae.getUsername();
            //获取表单数据作为参数
            GenerWork generWork=generWorkMapper.getGenerWorkByInst(procInstId);
            Map<String,Object> params=(Map) JSONObject.parseObject(generWork.getData_json());
            params.put("proce_inst_id",procInstId);
            params.put("user_name",username);
            Iterator<Map.Entry<String, Object>> iterator = params.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, Object> entry = iterator.next();
                sql=sql.replace("@"+entry.getKey(),entry.getValue().toString());
            }
            List<Map<String, Object>> mapList=sqlMapper.list(sql,null);
            for(Map<String, Object> map:mapList){
                nextWorkEmp += map.get("username").toString() + ",";
            }
        }
        if(!StringUtils.isNull(nextWorkEmp)){
            nextWorkEmp = nextWorkEmp.substring(0, nextWorkEmp.length() - 1);
        }

        //放入缓存，用于发送消息推送
        variables.put(taskid + "_bySql", nextWorkEmp);
        if(redisUtils.exists(procInstId+"_sms")){
            redisUtils.remove(procInstId+"_sms");
        }
        redisUtils.set(procInstId+"_sms",nextWorkEmp);
        return variables;
    }
    @Override
    public String getType(){
        return "bySql";
    }
}
